package com.atguigu.tingshu.album.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.model.album.BaseAttribute;
import com.atguigu.tingshu.model.album.BaseCategory1;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"all"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

	@Autowired
	private BaseCategory1Mapper baseCategory1Mapper;

	@Autowired
	private BaseCategory2Mapper baseCategory2Mapper;

	@Autowired
	private BaseCategory3Mapper baseCategory3Mapper;


	@Autowired
	private BaseCategoryViewMapper baseCategoryViewMapper;

	@Autowired
	private BaseAttributeMapper baseAttributeMapper;


	@Override
	public List<JSONObject> getBaseCategoryList() {
		//创建JSONObject对象的集合，将数据封装到集合中，返回
		List<JSONObject> list1 = new ArrayList<>();

		//获取全部的数据
		List<BaseCategoryView> baseCategoryViews = baseCategoryViewMapper.selectList(null);

		//将数据判断，如果为空，返回空集合，如果不为空，根据id进行分组，拿到对应的map
		if (CollectionUtils.isEmpty(baseCategoryViews)){
			return new ArrayList();
		}

		Map<Long, List<BaseCategoryView>> longListMap1 = baseCategoryViews.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));

		//遍历这个map集合
		Set<Map.Entry<Long, List<BaseCategoryView>>> getCategory1Entries = longListMap1.entrySet();
		Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator1 = getCategory1Entries.iterator();
		while (iterator1.hasNext()){
			Map.Entry<Long, List<BaseCategoryView>> next = iterator1.next();
			//拿到对应的key， getCategory1Id
			Long category1Id = next.getKey();
			//拿到对应的value，List<BaseCategoryView>
			List<BaseCategoryView> baseCategoryViewList = next.getValue();
			//创建JSONObject对象，进行封装categoryName，categoryId，categoryChild暂时没有办法封装
			JSONObject jsonObject1 = new JSONObject();
			jsonObject1.put("categoryId", category1Id);
			jsonObject1.put("categoryName", baseCategoryViewList.get(0).getCategory1Name());

			//通过value拿到二级数据，并且根据getCategory2Id进行分组并且进行遍历
			Map<Long, List<BaseCategoryView>> longListMap2 = baseCategoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
			Set<Map.Entry<Long, List<BaseCategoryView>>> getCategory2Entries = longListMap2.entrySet();
			Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator2 = getCategory2Entries.iterator();
			//创建JSONObject类型的集合，用来封装二级数据
			ArrayList<JSONObject> list2 = new ArrayList<>();
			while (iterator2.hasNext()){
				Map.Entry<Long, List<BaseCategoryView>> next1 = iterator2.next();
				//拿到对应的key， getCategory2Id
				Long category2Id = next1.getKey();
				//拿到对应的value，List<BaseCategoryView>，此时是二级数据
				List<BaseCategoryView> baseCategoryViewList2 = next1.getValue();
				//创建JSONObject对象，封装二级数据，进行封装categoryName，categoryId，categoryChild暂时没有办法封装
				JSONObject jsonObject2 = new JSONObject();
				jsonObject2.put("categoryId", category2Id);
				jsonObject2.put("categoryName", baseCategoryViewList.get(0).getCategory2Name());

				list2.add(jsonObject2);

				//直接拿到三级数据，不需要使用分组，直接遍历，然后进行封装
				List<JSONObject> list3 = new ArrayList<>();
				for (BaseCategoryView baseCategoryView : baseCategoryViewList2) {
					JSONObject jsonObject3 = new JSONObject();
					jsonObject3.put("categoryId", baseCategoryView.getCategory3Id());
					jsonObject3.put("categoryName", baseCategoryView.getCategory3Name());
					list3.add(jsonObject3);
				}
				jsonObject2.put("categoryChild", list3);
			}
			jsonObject1.put("categoryChild", list2);
			list1.add(jsonObject1);
		}
		return list1;
	}
}
